
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item label="模版名称:" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入模版名称" />
          </el-form-item>
          <el-form-item label="快递公司代码:" prop="code_id">
            <el-select
              v-model="ruleForm.code_id"
              style="width:100%"
              placeholder="请选择快递公司代码"
            >
              <el-option
                v-for="(item,index) in expressList"
                :key="index"
                :label="item.company_code"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模版地址:" prop="url">
            <el-input v-model="ruleForm.url" placeholder="请输入模版地址" />
          </el-form-item>
          <el-form-item label="状态:" prop="status">
            <el-select
              v-model="ruleForm.status"
              style="width:100%"
              placeholder="请选择状态"
            >

              <el-option
                label="启用"
                :value="1"
              />
              <el-option
                label="禁用"
                :value="2"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createTemplate, updateTemplate } from '@/api/base/template'
import { listCourierNo } from '@/api/base/express'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      showDialog: false,
      codes: [],
      expressList: [],
      ruleForm: {
        'name': '',
        'code_id': '',
        'url': '',
        'status': ''
      },
      rules: {
        name: [
          { required: true, message: '模版名称', trigger: 'blur' }
        ],
        code_id: [
          { required: true, message: '请选择快递公司代码' }
        ],
        url: [
          { required: true, message: '请输入模版地址' }
        ],
        status: [
          { required: true, message: '请选择状态' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        this.codes = []
        if (this.status === 2) {
          this.ruleForm = this.detial
        }
      }
    }
  },
  mounted() {
    this.getExpressCode()
  },
  methods: {
    getExpressCode() {
      listCourierNo({}).then(res => {
        this.expressList = res.data
      })
    },
    close() {
      this.$emit('update', false)
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.ruleForm = {
        'name': '',
        'code_id': '',
        'url': '',
        'status': ''
      }
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createTemplate(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateTemplate({
            'id': this.ruleForm.id,
            'name': this.ruleForm.name,
            'code_id': this.ruleForm.code_id,
            'url': this.ruleForm.url,
            'status': this.ruleForm.status
          }).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
